package com.sjf.JavaNIO.chatroom.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

import static com.sjf.JavaNIO.chatroom.server.ChatServer.readOperator;

/**
 * 聊天室客户端
 *
 * @author <huangtang>
 * @since 2022/4/16 16:03
 **/
public class ChatClient {

    //启动客户端方法
    public void startClient ( String name ) throws Exception {
        //连接服务端
        SocketChannel socketChannel = SocketChannel.open (new InetSocketAddress ("127.0.0.1" , 8888));
        //接收服务端的响应信息
        Selector selector = Selector.open ();
        socketChannel.configureBlocking (false);
        socketChannel.register (selector , SelectionKey.OP_READ);
        //新建线程
        new Thread (() -> {
            while (true) {
                try {
                    //获取channel数量
                    if ( selector.select () == 0 ) continue;

                    //获取可用的Key
                    Set<SelectionKey> selectionKeys = selector.selectedKeys ();
                    Iterator<SelectionKey> iterator = selectionKeys.iterator ();
                    //遍历集合
                    while (iterator.hasNext ()) {
                        SelectionKey selectionKey = iterator.next ();

                        //移除set集合中当前selectionKey
                        iterator.remove ();
                        //5 根据就绪的状态进行相应的操作
                        if ( selectionKey.isReadable () ) {
                            //read状态
                            readOperator (selector , selectionKey , 1);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace ();
                }
            }
        }).start ();
        //向服务器发送消息
        Scanner sc = new Scanner (System.in);
        while (sc.hasNextLine ()) {
            //需要发送的消息
            String sendMsg = name + ":" + sc.nextLine ();
            if ( sendMsg.length () > 0 ) {
                socketChannel.write (StandardCharsets.UTF_8.encode (sendMsg));
            }
        }

    }

    private String name;

    public ChatClient () {
        this.name = name;
    }

    public static void main ( String[] args ) {
        try {
            new ChatClient ().startClient ("sjf");
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
}

//ByteBuffer byteBuffer = ByteBuffer.allocate (1024);
//int readLength = socketChannel.read (byteBuffer);
//    byteBuffer.flip ();
//            while(readLength > 0){
//            CharBuffer charBuffer = StandardCharsets.UTF_8.decode (byteBuffer);
//            while(charBuffer.hasRemaining ()){
//            System.out.print (charBuffer.get ());
//            }
//            System.out.println ();
//            readLength = socketChannel.read (byteBuffer);
//            }